package com.syqc.videos

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.PopupWindow
import com.syqc.monitor.databinding.PopupTalkBinding

class TalkPopup(context: Context, private val callback: () -> Unit) : PopupWindow() {
    val binding = PopupTalkBinding.inflate(LayoutInflater.from(context))

    init {
        contentView = binding.root
        isClippingEnabled = false
        isFocusable = true //popupwindow设置焦点
        isOutsideTouchable = false //点击外面窗口消失
        width = LinearLayout.LayoutParams.MATCH_PARENT //设置SelectPicPopupWindow弹出窗体的宽
        height = LinearLayout.LayoutParams.MATCH_PARENT //设置SelectPicPopupWindow弹出窗体的高
        initView()
    }

    override fun dismiss() {
        val stackTrace = Exception().stackTrace
        if (stackTrace.size >= 2 && "dispatchKeyEvent" == stackTrace[1].methodName) {
            //按了返回键
        } else {
            super.dismiss()
        }
    }

    fun initView() {
        binding.llRealVideoTalk.setOnClickListener {
            dismiss()
            callback.invoke()
        }
    }

    fun setVoiceTitle(titleId: Int) {
        binding.tvAudio.setText(titleId)
    }

    fun setVoiceTitle(title: String) {
        binding.tvAudio.text = title
    }

    fun setVoiceImage(int: Int) {
        binding.ivAudio.setImageResource(int)
    }

    fun shouPopup(view: View) {
        binding.ivAudio.setImageResource(TalkHelper.voiceImg[0])
        showAtLocation(view, Gravity.NO_GRAVITY, 0, 0)
    }
}